分享

使用JNI调用FFmpeg解码音频并输出到AudioTrack求助

 mediatv 2016-07-23
2
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package codes.orion.swplayer;
 
import java.io.IOException;
 
import android.media.AudioTrack;
import android.media.AudioFormat;
import android.os.Handler;
import android.util.Log;
 
 
public class MediaObj {
    private AudioTrack audioTrack;
    private Handler handler = new Handler();
    private Runnable updateThread = new Runnable(){
        public void run() {
                byte[] bytes=stream(minbufsize);
                int dsize=data_size();
                if(audioTrack.write(bytes, 0, dsize)<dsize) {
                    Log.w(null,"Data not written completely");
                }
            handler.postDelayed(updateThread,50);
        }
    };
     
    private int samplerate,channeltype,minbufsize;
     
    public void setDataSource(String sourceFile) throws IOException {
        openFile(sourceFile);
        samplerate=getSampleRate();
        if (getChannel()==1)
            channeltype=AudioFormat.CHANNEL_OUT_MONO;
        else
            channeltype=AudioFormat.CHANNEL_OUT_STEREO;
        minbufsize=AudioTrack.getMinBufferSize (samplerate, channeltype, AudioFormat.ENCODING_PCM_16BIT);
        audioTrack = new AudioTrack(android.media.AudioManager.STREAM_MUSIC,samplerate,channeltype,
                AudioFormat.ENCODING_PCM_16BIT,
                minbufsize*2,
                AudioTrack.MODE_STREAM);
    }
     
    public void prepare() {
    }
     
    public void play() {
        handler.post(updateThread);
        audioTrack.play();
    }
     
    public MediaObj() {
        JNIinit();
    }
     
    @Override
    protected void finalize() throws Throwable {
        JNIexit();
        handler.removeCallbacks(updateThread);
        super.finalize();
    }
     
    public native byte[] stream(int minbufsize);
    public native int data_size();
    public native void openFile(String sourceFile) throws IOException;
    public native int getSampleRate();
    public native int getChannel();
    public native void JNIinit();
    public native void JNIexit();
    static {
        System.loadLibrary("mediaobj-jni");
    }
     
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多