gpt4 book ai didi

java - 在Android Studio中按下按钮时播放音频/声音问题

转载 作者:行者123 更新时间:2023-12-03 01:51:31 25 4
gpt4 key购买 nike

对于android world来说是很新的东西,单击按钮时播放音频时出现问题。它的有趣/怪异的方面是相同的代码对我的mainactivity起作用,但对我设置的secondactivity不起作用。我正在使用与mainactivity相同的确切代码。我在mainactivity上使用该代码只是为了对其进行测试,请记住,在mainactivity中尚未声明或定义任何媒体播放器。我这样做只是为了测试代码是否有效。

这是我的xml:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15sp"
android:layout_marginBottom="15sp" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press button to play audio"
android:textSize="40sp"
android:textColor="#ffff"
android:fontFamily="cursive"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"

/>


</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10sp"
android:layout_gravity="center"

>


<Button
android:id="@+id/AudioButton"
android:layout_width="wrap_content"
android:layout_height="50sp"
android:text="play"
android:textSize="22sp"
android:textColor="#ffff"
android:layout_marginRight="10dp"




/>

</LinearLayout>

这是JAVA:
package nameiscleared;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class SecondActivity extends AppCompatActivity {



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

Button start = (Button) findViewById(R.id.AudioButton);




start.setOnClickListener(new View.OnClickListener() {
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.audioname);

@Override
public void onClick(View view) {


mp.start();
}
});
}
}

最佳答案

这只是我的假设,我认为您在MainActivity中使用MediaPlayer时不会释放它。这就是为什么它不能在secondActivity上运行的原因。另一个错误是,MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.audioname);必须在onClick中,而不是在View.OnClickListener()中。您需要记住,在使用MediaPlayer之后,需要在不再使用它时将其释放。

A MediaPlayer can consume valuable system resources. Therefore, you should always take extra precautions to make sure you are not hanging on to a MediaPlayer instance longer than necessary. When you are done with it, you should always call release() to make sure any system resources allocated to it are properly released. For example, if you are using a MediaPlayer and your activity receives a call to onStop(), you must release the MediaPlayer, because it makes little sense to hold on to it while your activity is not interacting with the user (unless you are playing media in the background, which is discussed in the next section). When your activity is resumed or restarted, of course, you need to create a new MediaPlayer and prepare it again before resuming playback - Android Developers documentation.



正确的实现应该是这样的;

MainActivity
public class MainActivity extends AppCompatActivity{

private Button playBtn, startActivityBtn;
private MediaPlayer mediaPlayer;

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



playBtn = (Button)findViewById(R.id.playBtn);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(MainActivity.this, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
});

startActivityBtn = (Button)findViewById(R.id.startActivity);
startActivityBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}

@Override
protected void onStop() {
super.onStop();

if(null != mediaPlayer){

if(mediaPlayer.isPlaying())
mediaPlayer.stop();

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

SecondActivity
public class SecondActivity extends AppCompatActivity {

private Button playBtn;
private MediaPlayer mediaPlayer;

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

playBtn = (Button)findViewById(R.id.playBtn);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer = MediaPlayer.create(SecondActivity.this, Settings.System.DEFAULT_RINGTONE_URI);
mediaPlayer.start();
}
});

}

@Override
protected void onStop() {
super.onStop();

if(null != mediaPlayer){
if(mediaPlayer.isPlaying())
mediaPlayer.stop();

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

我不包括布局,因为这两个布局都非常简单。 MainActivity有两个按钮,用于播放和启动另一个 Activity 。 SecondActivity只有播放按钮。

关于java - 在Android Studio中按下按钮时播放音频/声音问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339328/

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