gpt4 book ai didi

Android MediaPlayer单例

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:40 27 4
gpt4 key购买 nike

我在这里阅读了几篇建议使用单例代码的文章,我已经阅读并放入音板,因为我想使用单例代码只允许一个媒体播放器实例,无论用户定义了多少次点击.

基本上我只想要一种声音,如果用户在播放期间单击另一个按钮,它会停止当前声音并播放按下的声音。我只想要一个 MediaPlayer 实例,但不知道如何实现它。

这是我的代码的基本思想:

    package com.example.context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;




import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class main extends Activity implements OnClickListener {

MediaPlayer player;

int[] ressound ={R.raw.boomstick, R.raw.chainsaw, R.raw.shebitch, R.raw.byebye,
R.raw.comegetsome, R.raw.groovy, R.raw.shoelace, R.raw.smart, R.raw.yeahbaby};//added as needed
int j=0;
private static final String TAG = "MyTag";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


//Coding for all buttons, registers, and Listeners
Button btn1 = (Button) findViewById(R.id.btn1);
registerForContextMenu(btn1);
btn1.setOnClickListener(this);
Button btn2 = (Button) findViewById(R.id.btn2);
registerForContextMenu(btn2);
btn2.setOnClickListener(this);
Button btn3 = (Button) findViewById(R.id.btn3);
registerForContextMenu(btn3);
btn3.setOnClickListener(this);

}

//On click Handlers for multiple buttons
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
// action to perform on button 1
j = 0;
playResource();
break;
case R.id.btn2:
// action to perform on button 1
j = 1;
playResource();
break;
case R.id.btn3:
// action to perform on button 1
j = 2;
playResource();
break;

}
public void playResource(int j, int resource) {
this.j = j;
if (player != null) {
if (player.isPlaying())
player.stop();
player.reset();
//from MediaPlayer implementation (link above)
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
if (afd == null) return;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
}
}
else {
//player is null
//it will create new MediaPlayer instance, setDataSource and call prepare
player = MediaPlayer.create(this, resource);
}
//if everything ok play file
//in case of any error return from method before (catch)
player.start();
}

这也是我必须更新和更改我的代码的地方,但它给我带来了传递 playresource() 函数的问题。我是不是传错了,传的应该是private吧。

最佳答案

为此您不需要单例。你在做什么是每次你想播放一个文件时调用创建方法。这是错误的,因为您已经拥有 MediaPlayer 实例。检查MediaPlayer.create implementation .我会这样做:

  • 创建新方法 playResource(int j, int resource)
  • 在每种情况下,我都会调用 R.id.btnX 方法 playResource(X, R.raw.Y) - X 和 Y 取决于 btn

playResource 方法示例:

private static final String TAG = "MyTag";
playResource(int j, int resource) {
this.j = j;
if (player != null) {
if (player.isPlaying())
player.stop();
player.reset();
//from MediaPlayer implementation (link above)
try {
AssetFileDescriptor afd = getResources().openRawResourceFd(resource);
if (afd == null) return null;
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
player.prepare();
} catch (IOException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (IllegalArgumentException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
} catch (SecurityException ex) {
Log.d(TAG, "create failed:", ex);
// failed: return
}
}
else {
//player is null
//it will create new MediaPlayer instance, setDataSource and call prepare
player = MediaPlayer.create(this, resource);
}
//if everything ok play file
//in case of any error return from method before (catch)
player.start();
}

当你不再需要MediaPlayer时,你应该释放它。例如在 onPause() 调用中:

if (player != null) {
player.release();
player = null;
}

我没有测试过,所以可能会有错误。希望对您有所帮助。

关于Android MediaPlayer单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7831320/

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