gpt4 book ai didi

java - 设置媒体 Controller

转载 作者:行者123 更新时间:2023-12-01 21:32:50 25 4
gpt4 key购买 nike

我正在尝试设置 MediaController,以便在播放音频时可以进行控制,但是当我尝试声明它时,“this”将作为错误出现。我做错了什么?

@TargetApi(Build.VERSION_CODES.LOLLIPOP)公共(public)类 MainActivity 扩展 AppCompatActivity 实现 MediaRecorder.OnInfoListener {

private boolean cont;
private MediaRecorder mediaRecorder;
private MediaPlayer mediaPlayer;
private String OUTPUT_FILE;
private MediaController mediaController;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

OUTPUT_FILE = Environment.getExternalStorageDirectory()+"/androidaudio1.3gpp";
cont=true;


}



public void buttonClicked(View view){


switch (view.getId()){

case R.id.startRec:
beginRecord();
break;

case R.id.stopRec:
stopRecord();
break;

case R.id.startPlay:
try {
begginPlaying();
} catch (IOException e) {
e.printStackTrace();
}
break;

case R.id.stopPlay:
stopPlaying();
break;


}
}


@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void begginPlaying() throws IOException {

if (mediaPlayer != null)
mediaPlayer.release();

mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(OUTPUT_FILE);

mediaController = new MediaController(this);
mediaController.setMediaPlayer(this);

mediaController.show();

mediaPlayer.prepare();
mediaPlayer.start();


}


private void stopPlaying(){

if (mediaPlayer!= null){

mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}


@Override
public void start() {
mediaPlayer.start();

}

@Override
public void pause() {
mediaPlayer.pause();

}

@Override
public int getDuration() {
return mediaPlayer.getDuration();
}

@Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}

@Override
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}

@Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}

@Override
public int getBufferPercentage() {
return 0;
}

@Override
public boolean canPause() {
return true;
}

@Override
public boolean canSeekBackward() {
return true;
}

@Override
public boolean canSeekForward() {
return true;
}

@Override
public int getAudioSessionId() {
return 0;
}

}

最佳答案

MediaController 需要将上下文传递给构造函数。如果没有更多代码或不知道在哪里声明 MediaController,我最好的猜测是您无权访问声明 MediaController 的位置的上下文。从您想要使用 MediaController 的 Activity 传递上下文,或者将此代码放入该 Activity 中。

https://developer.android.com/reference/android/widget/MediaController.html

编辑:

我尝试了你的代码,检查你的导入并确保你有,导入android.widget.MediaController而不是导入android.media.session.MediaController

关于java - 设置媒体 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com