Android工作室播放音频文件而无需单击按钮

时间:2016-01-18 15:17:22

标签: android audio

我最近付钱给某人为我开发了一个实验性的应用程序。该应用程序基本上显示了一个带有几个"按钮的图片"在角落里有其他功能。基本上,我还希望在显示图像时自动播放音频文件,而不必单击按钮(此类功能已写入脚本)。我不是程序员(这就是为什么我不得不雇人),但他也无法弄清楚如何做到这一点。我只是想知道是否有人有任何我可以尝试的建议?

以下是控制音频文件的代码:     public class ObjectFragment扩展Fragment {

private int index;
private int trackID;
private int videoID;
private boolean visible;

private Integer[] imageIDs = {R.drawable.c1, R.drawable.c2, R.drawable.c3,
        R.drawable.k1, R.drawable.k2, R.drawable.k3,
        R.drawable.m1, R.drawable.m2, R.drawable.m3};

private Integer[] trackIDs = {R.raw.chatten, R.raw.chatten, R.raw.chatten,
        R.raw.kobo, R.raw.kobo, R.raw.kobo,
        R.raw.manu, R.raw.manu, R.raw.manu};

private Integer[] videos = {R.raw.chatten_vid2, R.raw.kobo_vid2, R.raw.manu_vid2};

static ObjectFragment newInstance(int i) {
    ObjectFragment f = new ObjectFragment();
    Bundle args = new Bundle();
    args.putInt("index", i);
    f.setArguments(args);
    return f;
}

/**
 * When creating, retrieve this instance's number from its arguments.
 */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    index = getArguments() != null ? getArguments().getInt("index") : 0;
    trackID = getArguments() != null ? getArguments().getInt("index") : 0;
}

@SuppressLint("SetTextI18n")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    visible = true;

    final View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
    final Button videoButton = (Button) rootView.findViewById(R.id.video);
    videoButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(MotionEventCompat.getActionMasked(motionEvent)==MotionEvent.ACTION_DOWN)
                MainActivity.getPerson().formatString(System.nanoTime()-MainActivity.initTime, "button", motionEvent.getX(), motionEvent.getY(), "video");
            return false;
        }
    });
    final VideoView videoView = (VideoView) rootView.findViewById(R.id.videoView);
    final TextView tv = (TextView) rootView.findViewById(R.id.word);
    final int word;


    ((ImageView) rootView.findViewById(R.id.imageView)).setImageResource(imageIDs[index]);

    //Change values here!!!
    if(index<4) {
        word=3;
        tv.setText("Chatten");
        videoButton.setBackgroundResource(R.drawable.snapchatten2);
        videoID=0;
    }
    else if(index <8) {
        word=7;
        tv.setText("Kobo");
        videoButton.setBackgroundResource(R.drawable.snapkobo2);
        videoID=1;
    }
    else {
        word=11;
        tv.setText("Manu");
        videoButton.setBackgroundResource(R.drawable.snapmanu2);
        videoID=2;
    }

    rootView.findViewById(R.id.abc).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            String text="";
            if(word==3)
                text = "Chatten";
            else if(word==7)
                text = "Kobo";
            else if(word==11)
                text="Manu";
            if(MotionEventCompat.getActionMasked(motionEvent)==MotionEvent.ACTION_DOWN)
                MainActivity.getPerson().formatString(System.nanoTime()-MainActivity.initTime, "button", motionEvent.getX(), motionEvent.getY(), "abc "+text);
            return false;
        }
    });
    rootView.findViewById(R.id.abc).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            tv.setVisibility(View.VISIBLE);
            MainActivity.music.stop();
            MainActivity.music.doInBackground(new Object[]{trackIDs[trackID]});
            MainActivity.music.setLooping(false);
            MainActivity.music.play();
            while (!MainActivity.playBgMusic()) ;
        }
    });

    rootView.findViewById(R.id.babylabs).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (MotionEventCompat.getActionMasked(motionEvent) == MotionEvent.ACTION_DOWN)
                MainActivity.getPerson().formatString(System.nanoTime() - MainActivity.initTime, "button", motionEvent.getX(), motionEvent.getY(), "babylabs");
            return false;
        }
    });
    rootView.findViewById(R.id.babylabs).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (tv.getVisibility() == View.VISIBLE) {
                tv.setVisibility(View.GONE);
                return;
            }
            visible = false;
            MainActivity.back_counter = 1;
            getActivity().onBackPressed();
        }
    });

    videoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            videoButton.setVisibility(View.INVISIBLE);
            MainActivity.music.pause();
            rootView.findViewById(R.id.imageView).setVisibility(View.INVISIBLE);
            videoView.setVisibility(View.VISIBLE);
            videoView.setVideoURI(Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + videos[videoID]));

            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    videoView.start();
                }
            });

            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    videoView.setVisibility(View.GONE);
                    videoButton.setVisibility(View.VISIBLE);
                    rootView.findViewById(R.id.imageView).setVisibility(View.VISIBLE);
                    MainActivity.music.play();
                }
            });
        }
    });

    return rootView;
}

}

1 个答案:

答案 0 :(得分:0)

使用ViewTreeObserver解决方案的代码段。它会告诉您何时绘制图像。

echo implode("\n", output_myarray($myarray), true);

您可以将此代码添加到oncreate方法中。不要忘记以前准备好你的播放器。也许你想添加一个onloadcomplete监听器。