จัดการสถานะและทรัพยากรของ MediaPlayer
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เอกสารนี้ครอบคลุม 2 ด้านที่อาจเกิดข้อผิดพลาด
รัฐ เมื่อใช้ "Medialayer" การดำเนินการบางอย่างจะใช้ได้เฉพาะในสถานะบางอย่างเท่านั้น การดำเนินการที่ไม่ถูกต้องอาจทำให้เกิดข้อยกเว้นหรือลักษณะการทำงานที่ไม่คาดคิด
ทรัพยากร เมื่อทําการเปลี่ยนแปลงการกําหนดค่า เช่น การหมุนหน้าจอ คุณต้องปล่อยออบเจ็กต์ MediaPlayer
เพื่อเพิ่มพื้นที่ว่างในทรัพยากรของระบบและหลีกเลี่ยงการหมดทรัพยากร
จัดการสถานะ
MediaPlayer
อิงตามสถานะ กล่าวคือ มีสถานะภายในที่คุณต้องคำนึงถึงเสมอเมื่อเขียนโค้ด เนื่องจากการดำเนินการบางอย่างจะถูกต้องก็ต่อเมื่อผู้เล่นอยู่ในสถานะหนึ่งๆ หากคุณดำเนินการขณะอยู่ในสถานะที่ไม่ถูกต้อง ระบบอาจแสดงข้อยกเว้นหรือทําให้ระบบทำงานผิดพลาด
แผนภาพสถานะในเอกสารประกอบของคลาส MediaPlayer
จะชี้แจงว่าเมธอดใดที่ย้าย MediaPlayer
จากสถานะหนึ่งไปยังอีกสถานะหนึ่ง เช่น
เมื่อถึงจุดนั้น คุณจะสลับระหว่างสถานะ Started
,
Paused
และ PlaybackCompleted
ได้โดยเรียกใช้เมธอดต่างๆ เช่น start()
, pause()
และ seekTo()
เป็นต้น
อย่างไรก็ตาม เมื่อคุณเรียกใช้ stop()
คุณจะเรียกใช้ start()
ไม่ได้อีกจนกว่าจะเตรียม MediaPlayer
อีกครั้ง
อย่าลืมคำนึงถึงแผนภาพสถานะเสมอเมื่อเขียนโค้ดที่โต้ตอบกับออบเจ็กต์ MediaPlayer
เนื่องจากการเรียกเมธอดจากสถานะที่ผิดเป็นสาเหตุที่พบบ่อยของข้อบกพร่อง
ปล่อย MediaPlayer
MediaPlayer
อาจใช้ทรัพยากรระบบที่มีค่า ดังนั้น คุณจึงควรใช้ความระมัดระวังเพิ่มเติมเสมอเพื่อให้แน่ใจว่าคุณไม่ได้เก็บอินสแตนซ์ MediaPlayer
ไว้นานกว่าที่จำเป็น เมื่อดำเนินการเสร็จแล้ว คุณควรเรียกใช้ release()
เสมอเพื่อให้แน่ใจว่าระบบได้ปล่อยทรัพยากรทั้งหมดที่กําหนดไว้ให้กับ release()
ดังกล่าวแล้ว
ตัวอย่างเช่น หากคุณใช้ MediaPlayer
และกิจกรรมได้รับการเรียกใช้ onStop()
คุณต้องปล่อย MediaPlayer
เนื่องจากไม่มีเหตุผลที่จะเก็บไว้ในขณะที่กิจกรรมไม่ได้โต้ตอบกับผู้ใช้ (เว้นแต่คุณจะเล่นสื่อในเบื้องหลัง ซึ่งจะกล่าวถึงในส่วนถัดไป)
เมื่อกิจกรรมกลับมาดำเนินต่อหรือเริ่มใหม่ คุณจะต้องสร้าง MediaPlayer
ใหม่และเตรียมอีกครั้งก่อนเล่นต่อ
วิธีปล่อยและทำให้ MediaPlayer
ของคุณเป็นโมฆะมีดังนี้
Kotlin
mediaPlayer?.release()
mediaPlayer = null
Java
mediaPlayer.release();
mediaPlayer = null;
ตัวอย่างเช่น ลองพิจารณาปัญหาที่อาจเกิดขึ้นหากคุณลืมปล่อยMediaPlayer
เมื่อกิจกรรมหยุดลง แต่สร้างใหม่เมื่อกิจกรรมเริ่มขึ้นอีกครั้ง เมื่อผู้ใช้เปลี่ยนการวางแนวหน้าจอ (หรือเปลี่ยนการกำหนดค่าอุปกรณ์ด้วยวิธีอื่น) ระบบจะรีสตาร์ทกิจกรรมโดยค่าเริ่มต้น คุณอาจใช้ทรัพยากรของระบบทั้งหมดอย่างรวดเร็วเมื่อผู้ใช้หมุนอุปกรณ์ไปมาระหว่างแนวตั้งและแนวนอน เนื่องจากเมื่อเปลี่ยนการวางแนวแต่ละครั้ง คุณสร้าง MediaPlayer
ใหม่ที่ไม่ได้เผยแพร่
ดูข้อมูลเพิ่มเติมเกี่ยวกับการเริ่มรันไทม์ใหม่ได้ที่การจัดการการเปลี่ยนแปลงรันไทม์
คุณอาจสงสัยว่าจะเกิดอะไรขึ้นหากต้องการเล่น "สื่อในเบื้องหลัง" ต่อแม้ว่าผู้ใช้จะออกจากกิจกรรมแล้ว เช่นเดียวกับลักษณะการทํางานของแอปพลิเคชันเพลงในตัว ในกรณีนี้ สิ่งที่คุณต้องการคือ MediaPlayer
ที่ควบคุมโดยบริการตามที่อธิบายไว้ในส่วนถัดไป
ดูข้อมูลเพิ่มเติม
Jetpack Media3 เป็นโซลูชันที่แนะนำสำหรับการเล่นสื่อในแอป อ่านข้อมูลเพิ่มเติมเกี่ยวกับโซลูชันนี้
หน้าเหล่านี้ครอบคลุมหัวข้อเกี่ยวกับการบันทึก การจัดเก็บ และการเล่นเสียงและวิดีโอ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Manage MediaPlayer state and resources\n\nThis document covers two areas with potential pitfalls.\n\n- **State.** With \"Medialayer\\`, certain operations are valid only in specific\n states. Incorrect operations can cause exceptions or unexpected behavior.\n\n- **Resources** When you make configuration changes, such as screen rotation,\n You must release a `MediaPlayer` object to free up system resources and\n avoid resource exhaustion.\n\nManage state\n------------\n\n[`MediaPlayer`](/reference/android/media/MediaPlayer) is state-based. That is, it has an\ninternal state that you must\nalways be aware of when you write your code, because certain operations are only\nvalid when the player is in specific states. If you perform an operation while\nin the wrong state, the system may throw an exception or cause other undesirable\nbehaviors.\n\nThe state diagram in the [`MediaPlayer`](/reference/android/media/MediaPlayer) class documentation clarifies\nwhich methods move the [`MediaPlayer`](/reference/android/media/MediaPlayer) from one state\nto another. For example:\n\n- When you create a new [`MediaPlayer`](/reference/android/media/MediaPlayer), it is in the *Idle* state.\n- You initialize it by calling[`setDataSource()`](/reference/android/media/MediaPlayer#setDataSource(android.content.Context,%20android.net.Uri)), which changes it to the *Initialized* state.\n- You prepare it using either the [`prepare()`](/reference/android/media/MediaPlayer#prepare()) or [`prepareAsync()`](/reference/android/media/MediaPlayer#prepareAsync()) method.\n- When the [`MediaPlayer`](/reference/android/media/MediaPlayer) is done preparing, it enters the `Prepared` state, which means you can call [`start()`](/reference/android/media/MediaPlayer#start()) to make it play the media.\n\nAt that point, as the diagram illustrates, you can move between the `Started`,\n`Paused` and `PlaybackCompleted` states by calling such methods as\n[`start()`](/reference/android/media/MediaPlayer#start()), [`pause()`](/reference/android/media/MediaPlayer#pause()), and [`seekTo()`](/reference/android/media/MediaPlayer#seekTo(int)), among others.\n\nWhen you call [`stop()`](/reference/android/media/MediaPlayer#stop()), however, notice that you cannot call [`start()`](/reference/android/media/MediaPlayer#start())\nagain until you prepare the [`MediaPlayer`](/reference/android/media/MediaPlayer) again.\n\nAlways keep [the state diagram](/static/images/mediaplayer_state_diagram.gif) in mind when writing code that interacts with\na [`MediaPlayer`](/reference/android/media/MediaPlayer) object, because calling its methods from the wrong state is\na common cause of bugs.\n\nRelease the MediaPlayer\n-----------------------\n\nA [`MediaPlayer`](/reference/android/media/MediaPlayer) can consume valuable system resources. Therefore, you\nshould always take extra precautions to make sure you are not hanging on to a\n[`MediaPlayer`](/reference/android/media/MediaPlayer) instance longer than necessary. When you are done with it,\nyou should always call [`release()`](/reference/android/media/MediaPlayer#release()) to make sure any system resources\nallocated to it are properly released.\n\nFor example, if you are using a [`MediaPlayer`](/reference/android/media/MediaPlayer) and your activity receives a\ncall to [`onStop()`](/reference/android/app/Activity#onStop()), you must release the [`MediaPlayer`](/reference/android/media/MediaPlayer), because it\nmakes little sense to hold on to it while your activity is not interacting with\nthe user (unless you are playing media in the background, which is discussed in\nthe next section).\n\nWhen your activity is resumed or restarted, of course, you need to create a new\n[`MediaPlayer`](/reference/android/media/MediaPlayer) and prepare it again before resuming playback.\n\nHere's how you should release and then nullify your [`MediaPlayer`](/reference/android/media/MediaPlayer): \n\n### Kotlin\n\n mediaPlayer?.release()\n mediaPlayer = null\n\n### Java\n\n mediaPlayer.release();\n mediaPlayer = null;\n\nAs an example, consider the problems that arise if you forget to release the\n[`MediaPlayer`](/reference/android/media/MediaPlayer) when your activity stops, but create a new one when the\nactivity starts again. When the user changes the screen orientation (or changes\nthe device configuration in another way), the system restarts the activity by\ndefault. You might quickly consume all of the system resources as the user\nrotates the device back and forth between portrait and landscape, because at\neach orientation change, you create a new [`MediaPlayer`](/reference/android/media/MediaPlayer) that you never\nrelease.\n\nFor more information about runtime restarts, see [Handling Runtime Changes](/guide/topics/resources/runtime-changes).\n\nYou may be wondering what happens if you want to continue playing \"background\nmedia\" even when the user leaves your activity, much in the same way that the\nbuilt-in Music application behaves. In this case, what you need is a\n[`MediaPlayer`](/reference/android/media/MediaPlayer) controlled by a Service, as discussed in the next section\n\nLearn more\n----------\n\nJetpack Media3 is the recommended solution for media playback in your app. [Read\nmore](/media/media3) about it.\n\nThese pages cover topics relating to recording, storing, and playing back audio\nand video:\n\n- [Supported Media Formats](/guide/topics/media/media-formats)\n- [MediaRecorder](/guide/topics/media/mediarecorder)\n- [Data Storage](/guide/topics/data/data-storage)"]]