Java 安卓播放片段中的声音
我发现了多个类似的帖子,但一直无法解决,所以发布我的代码寻求帮助 我有3个片段,使用带有PagerAdapter的ViewPager允许在片段之间滑动。这个项目一开始没有任何片段,我能够播放从MainActivity中单击的按钮发出的声音 这是有效的代码 主要活动Java 安卓播放片段中的声音,java,android,android-fragments,android-mediaplayer,Java,Android,Android Fragments,Android Mediaplayer,我发现了多个类似的帖子,但一直无法解决,所以发布我的代码寻求帮助 我有3个片段,使用带有PagerAdapter的ViewPager允许在片段之间滑动。这个项目一开始没有任何片段,我能够播放从MainActivity中单击的按钮发出的声音 这是有效的代码 主要活动 import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bund
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
MediaPlayer example4Sound;
MediaPlayer example5Sound;
MediaPlayer example6Sound;
MediaPlayer example7Sound;
MediaPlayer example8Sound;
MediaPlayer example9Sound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
example1Sound = MediaPlayer.create(this, R.raw.example1);
example2Sound = MediaPlayer.create(this, R.raw.example2);
example3Sound = MediaPlayer.create(this, R.raw.example3);
example4Sound = MediaPlayer.create(this, R.raw.example4);
example5Sound = MediaPlayer.create(this, R.raw.example5);
example6Sound = MediaPlayer.create(this, R.raw.example6);
example7Sound = MediaPlayer.create(this, R.raw.example7);
example8Sound = MediaPlayer.create(this, R.raw.example8);
example9Sound = MediaPlayer.create(this, R.raw.example9);
}
public void playExampleSound1(View view) {
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
public void playExampleSound4(View view) {
example4Sound.start();
}
public void playExampleSound5(View view) {
example5Sound.start();
}
public void playExampleSound6(View view) {
example6Sound.start();
}
public void playExampleSound7(View view) {
example7Sound.start();
}
public void playExampleSound8(View view) {
example8Sound.start();
}
public void playExampleSound9(View view) {
example9Sound.start();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.anjosoft.MainActivity">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_column="0"
android:background="@drawable/button1"
android:onClick="playExampleSound1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:layout_column="1"
android:background="@drawable/button2"
android:onClick="playExampleSound2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton3"
android:layout_column="2"
android:background="@drawable/button3"
android:onClick="playExampleSound3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton4"
android:layout_column="0"
android:background="@drawable/button4"
android:onClick="playExampleSound4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton5"
android:layout_column="1"
android:background="@drawable/button5"
android:onClick="playExampleSound5" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton6"
android:layout_column="2"
android:background="@drawable/button6"
android:onClick="playExampleSound6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton7"
android:layout_column="0"
android:background="@drawable/button7"
android:onClick="playExampleSound7" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton8"
android:layout_column="1"
android:background="@drawable/button8"
android:onClick="playExampleSound8" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton9"
android:layout_column="2"
android:background="@drawable/button9"
android:onClick="playExampleSound9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sound"
android:id="@+id/checkBox"
android:layout_column="1" />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.anjosoft.MainActivity">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_column="0"
android:background="@drawable/button1"
android:onClick="playExampleSound1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:layout_column="1"
android:background="@drawable/button2"
android:onClick="playExampleSound2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton3"
android:layout_column="2"
android:background="@drawable/button3"
android:onClick="playExampleSound3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton4"
android:layout_column="0"
android:background="@drawable/button4"
android:onClick="playExampleSound4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton5"
android:layout_column="1"
android:background="@drawable/button5"
android:onClick="playExampleSound5" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton6"
android:layout_column="2"
android:background="@drawable/button6"
android:onClick="playExampleSound6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton7"
android:layout_column="0"
android:background="@drawable/button7"
android:onClick="playExampleSound7" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton8"
android:layout_column="1"
android:background="@drawable/button8"
android:onClick="playExampleSound8" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton9"
android:layout_column="2"
android:background="@drawable/button9"
android:onClick="playExampleSound9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sound"
android:id="@+id/checkBox"
android:layout_column="1" />
</TableRow>
</TableLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
fragnment_one_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#f70808">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_column="0"
android:background="@drawable/button1"
android:onClick="playExampleSound1"
android:nestedScrollingEnabled="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:layout_column="1"
android:background="@drawable/button2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton3"
android:layout_column="2"
android:background="@drawable/button3" />
</TableRow>
</TableLayout>
</RelativeLayout>
碎片装载时,声音会播放。如何使onclick工作?您可以为每个按钮使用onclick侦听器,它可以工作 我认为fragnment_one_layout.xml使用充气器链接到FragmentOne.java 但是fragment_one_layout.xml无法访问FragmentOne.java,因此 语句onClick=“methodName”无法访问所需的方法。
mediaPlayer对象与您的问题无关。您可以对每个按钮使用onClickListeners,它可以工作 我认为fragnment_one_layout.xml使用充气器链接到FragmentOne.java 但是fragment_one_layout.xml无法访问FragmentOne.java,因此 语句onClick=“methodName”无法访问所需的方法。
mediaPlayer对象与您的问题无关。您可以使用一个函数将声音从活动重定向到片段! 活跃
fun onSound(sound:Int){
mp = MediaPlayer.create(this,soundFile)
mp.start
}
从碎片
onResume{
(activity as ActivityName).onSound(R.raw.sound)
}
您可以制作一个函数,将声音从活动重定向到片段! 活跃
fun onSound(sound:Int){
mp = MediaPlayer.create(this,soundFile)
mp.start
}
从碎片
onResume{
(activity as ActivityName).onSound(R.raw.sound)
}
还添加了主要内容,即错误登录只是在模拟中。当我点击按钮1时,按钮1显示“不幸的是,SwipeDemo已停止”。单击“确定”。同时添加主要内容,即错误登录只是在模拟中。当我点击按钮1时,按钮1显示“不幸的是,SwipeDemo已经停止。”并单击“确定”。